﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.General;
    using YidanSoft.Library.EditorUtility.Win32;

    [ToolboxItem(false)]
    public class RadioButtonEx : RadioButton
    {
        private const int CIRCLE_DIAMETER = 11;
        private DrawState drawState = DrawState.Normal;

        public RadioButtonEx()
        {
            this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        }

        private void DrawCheckGlyph(Graphics g, DrawState state)
        {
            Rectangle clientRectangle = base.ClientRectangle;
            Point point = new Point(clientRectangle.Left + 4, ((clientRectangle.Top + clientRectangle.Top) + ((clientRectangle.Height - 4) / 2)) + 2);
            Point point2 = new Point(point.X + 4, point.Y);
            Point point3 = new Point(clientRectangle.Left + 6, (clientRectangle.Top + clientRectangle.Top) + ((clientRectangle.Height - 4) / 2));
            Point point4 = new Point(point3.X, point3.Y + 4);
            Color empty = Color.Empty;
            if (state == DrawState.Normal)
            {
                empty = Color.Black;
            }
            else if (state == DrawState.Hot)
            {
                empty = ColorUtil.VSNetBorderColor;
            }
            else if (state == DrawState.Disable)
            {
                empty = SystemColors.ControlDark;
            }
            using (Pen pen = new Pen(empty, 2f))
            {
                g.DrawLine(pen, point, point2);
                g.DrawLine(pen, point3, point4);
            }
        }

        private void DrawRadioButtonState(DrawState state)
        {
            Rectangle clientRectangle = base.ClientRectangle;
            IntPtr dC = WindowsAPI.GetDC(base.Handle);
            Rectangle rect = new Rectangle(clientRectangle.Left, clientRectangle.Top + ((clientRectangle.Height - 11) / 2), 11, 11);
            using (Graphics graphics = Graphics.FromHdc(dC))
            {
                graphics.FillEllipse(SystemBrushes.Window, rect);
                if (state == DrawState.Normal)
                {
                    graphics.DrawEllipse(Pens.Black, rect);
                }
                else if (state == DrawState.Hot)
                {
                    graphics.DrawEllipse(SystemPens.Highlight, rect);
                }
                else if (state == DrawState.Disable)
                {
                    graphics.DrawEllipse(SystemPens.ControlDark, rect);
                }
                if (base.Checked)
                {
                    this.DrawCheckGlyph(graphics, state);
                }
            }
            WindowsAPI.ReleaseDC(base.Handle, dC);
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this.drawState = DrawState.Hot;
            base.Invalidate();
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.drawState = DrawState.Normal;
            base.Invalidate();
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.drawState = DrawState.Hot;
            base.Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (!base.ContainsFocus)
            {
                this.drawState = DrawState.Normal;
                base.Invalidate();
            }
        }

        protected override void WndProc(ref Message m)
        {
            bool flag = true;
            if (m.Msg == 15)
            {
                base.WndProc(ref m);
                this.DrawRadioButtonState(base.Enabled ? this.drawState : DrawState.Disable);
                flag = false;
            }
            if (flag)
            {
                base.WndProc(ref m);
            }
        }

        public System.Windows.Forms.FlatStyle FlatStyle
        {
            get
            {
                return base.FlatStyle;
            }
            set
            {
                if (value != System.Windows.Forms.FlatStyle.Flat)
                {
                    string paramName = "FlatStyle needs to be set to Flat for this class";
                    ArgumentException exception = new ArgumentException("FlatStyle", paramName);
                    throw exception;
                }
                base.FlatStyle = value;
            }
        }
    }
}

